import 'package:flutter/material.dart';
import 'package:flutter_quick/Controller/im_controller.dart';
import 'package:flutter_quick/Interfaces/IM.dart';
import 'package:flutter_quick/components/chat-message/msg-item.dart';
import 'package:flutter_quick/components/chat-message/msg-text.dart';
import 'package:get/get.dart';

class ChatPage extends StatelessWidget {
  const ChatPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffededed),
      appBar: AppBar(
        foregroundColor: Colors.black,
        elevation: 0,
        title: const Text(
          'mspcPool',
          style: TextStyle(fontSize: 18),
        ),
        centerTitle: true,
        backgroundColor: const Color(0xffe5e3e4),
      ),
      body: Column(children: [
        Expanded(child: ChatList()),
        // 聊天输入框
        Container(
          padding: const EdgeInsets.only(left: 6, right: 6, top: 6, bottom: 12),
          child: Row(
            children: [
              const Icon(
                Icons.record_voice_over_outlined,
                size: 26,
                color: Color(0xff030303),
              ),
              Expanded(
                child: Container(
                  height: 36,
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  child: const TextField(
                    decoration: InputDecoration(
                      contentPadding:
                          EdgeInsets.symmetric(vertical: 0, horizontal: 10),
                      filled: true,
                      fillColor: Colors.white,
                      border: OutlineInputBorder(
                        borderSide: BorderSide.none,
                      ),
                    ),
                  ),
                ),
              ),
              const Icon(
                Icons.emoji_emotions_outlined,
                size: 26,
                color: Color(0xff030303),
              ),
              const SizedBox(width: 10),
              const Icon(
                Icons.add_circle_outline,
                size: 26,
                color: Color(0xff030303),
              ),
            ],
          ),
        ),
      ]),
    );
  }
}

class ChatList extends StatefulWidget {
  const ChatList({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return ChatListState();
  }
}

class ChatListState extends State<ChatList> {
  List<MessageItemItem> _messageList = [];

  @override
  void initState() {
    super.initState();
    _getData();
  }

  _getData() async {
    IMController imController = Get.find();
    List<MessageItemItem> list =
        await imController.im.getC2CHistoryMessageList();

    setState(() {
      _messageList = list;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
        padding: const EdgeInsets.only(top: 10),
        children: _messageList
            .map((e) => MsgItem(
                  avatar: e.avatar,
                  name: e.name,
                  content: MsgText(
                    child: Text(
                      e.textMessageContent?.text ?? '',
                      style: const TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                ))
            .toList());

    // [

    // ]
  }
}
